package com.kcwl.idea.plugins.tool.handler;

import com.kcwl.idea.plugins.base.constants.KcKeyManager;
import com.kcwl.idea.plugins.tool.Menu;
import com.kcwl.idea.plugins.tool.config.KcCryptSettings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.intellij.openapi.util.Pair;
import com.kcwl.idea.plugins.base.utils.*;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

/**
 * 手机号、身份证号、url请求加解密处理
 */
public class KcCryptHandler {

    private static final String SETTTING_MSG = "请配置密钥【Settings】-【Other Settings】-【KcCrypt Settings】";
    /**
     * 身份证号码加密
     *
     * @param input
     * @param output
     */
    public void idEncrypt(Menu menu, RSyntaxTextArea input, RSyntaxTextArea output) {
        Pair<String, CryptKey> pair = getSecretKeyObjectSerial(input);
        String id = pair.getFirst();
        if (!ValidationUtil.isIdCard(id)) {
            output.setText("身份证号码格式不正确");
            return;
        }
        CryptKey secretKey = pair.getSecond();
        if (secretKey == null || StrUtil.isBlank(secretKey.getIdCardNoKey())) {
            output.setText(SETTTING_MSG);
            return;
        }
        KcAesCrypt kcAesCrypt = new KcAesCrypt(secretKey.getIdCardNoKey());
        output.setText(kcAesCrypt.encrypt(id));
    }


    /**
     * 身份证号码解密
     *
     * @param input
     * @param output
     */
    public void idDecrypt(Menu menu, RSyntaxTextArea input, RSyntaxTextArea output) {
        Pair<String, CryptKey> pair = getSecretKeyObjectSerial(input);
        String id = pair.getFirst();
        CryptKey secretKey = pair.getSecond();
        if (secretKey == null || StrUtil.isBlank(secretKey.getIdCardNoKey())) {
            output.setText(SETTTING_MSG);
            return;
        }
        KcAesCrypt kcAesCrypt = new KcAesCrypt(secretKey.getIdCardNoKey());
        output.setText(kcAesCrypt.decrypt(id));
    }

    /**
     * 手机号加密
     *
     * @param input
     * @param output
     */
    public void mobileEncrypt(Menu menu, RSyntaxTextArea input, RSyntaxTextArea output) {
        Pair<String, CryptKey> pair = getSecretKeyObjectSerial(input);
        String mobile = pair.getFirst();
        CryptKey secretKey = pair.getSecond();
        if (!ValidationUtil.isSimpleMobile(mobile)) {
            output.setText("手机号码格式不正确");
            return;
        }
        if (secretKey == null || StrUtil.isBlank(secretKey.getMobileKey())) {
            output.setText(SETTTING_MSG);
            return;
        }
        KcMobileCrypt kcMobileCrypt = new KcMobileCrypt(secretKey.getMobileKey());
        output.setText(kcMobileCrypt.encrypt(mobile));
    }


    /**
     * 手机号解密
     *
     * @param input
     * @param output
     */
    public void mobileDecrypt(Menu menu, RSyntaxTextArea input, RSyntaxTextArea output) {
        Pair<String, CryptKey> pair = getSecretKeyObjectSerial(input);
        String mobile = pair.getFirst();
        CryptKey secretKey = pair.getSecond();
        KcMobileCrypt kcMobileCrypt = new KcMobileCrypt(secretKey.getMobileKey());
        if (secretKey == null || StrUtil.isBlank(secretKey.getMobileKey())) {
            output.setText(SETTTING_MSG);
            return;
        }
        output.setText(kcMobileCrypt.decrypt(mobile));
    }

    /**
     * 请求参数解密
     *
     * @param menu
     * @param input
     * @param output
     */
    public void paramDecrypt(Menu menu, RSyntaxTextArea input, RSyntaxTextArea output) {
        String decrypt = AESUtil.decrypt(input.getText().trim(), KcKeyManager.getInstance().getParamPrivateKey(menu.getCode()));
        // 可以同时解析数组或者Object
        JsonElement element = new JsonParser().parse(decrypt);;
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        output.setText(gson.toJson(element));
        // 格式化成功后定位到第一行
        output.setCaretPosition(0);
    }


    /**
     * 获取秘钥
     *
     * @return
     */
    private Pair<String, CryptKey> getSecretKeyObjectSerial(RSyntaxTextArea input) {
        String inputText = input.getText();
        if (StrUtil.isBlank(inputText)) {
            return null;
        }
        KcCryptSettings settings = KcCryptSettings.getInstance();
        String text = input.getText().trim();
        if (text.startsWith("test")) {
            text = text.replace("test", "");
            return new Pair<>(text, new CryptKey(settings.getMobileKeyTest(), settings.getIdCardKeyTest()));
        }
        return new Pair<>(text, new CryptKey(settings.getMobileKeyPro(), settings.getIdCardKeyPro()));
    }

    public static class CryptKey {
        private String mobileKey;
        private String idCardNoKey;

        public CryptKey(String mobileKey, String idCardNoKey) {
            this.mobileKey = mobileKey;
            this.idCardNoKey = idCardNoKey;
        }

        public String getMobileKey() {
            return mobileKey;
        }

        public String getIdCardNoKey() {
            return idCardNoKey;
        }
    }
}
